window.onload = function (){
	
	var goods_id = document.getElementById('goods_id').value;
	
	var history = getCookie("history");
	var historyIds = history != null ? history.split(",") : new Array();
	
	for (var i = 0; i < historyIds.length; i ++) {
		if (historyIds[i] == goods_id) {
			historyIds.splice(i, 1);
			break;
		}
	}
	historyIds.unshift(goods_id);

	if (historyIds.length > 6) {
		historyIds.pop();
	}

	addCookie("history", historyIds.join(","), '/');

	function getCookie(history) {
		if(history != null){
			var value = new RegExp("(?:^|; )" + encodeURIComponent(String(history)) + "=([^;]*)").exec(document.cookie);
			return value ? decodeURIComponent(value[1]) : null;
		}
	}

	function addCookie(history, value, options) {

		if(arguments.length > 1 && history != null){
			if(options == null){
				options = {};
			}
			if(value == null){
				options.expires = -1;
			}
			if(typeof options.expires == "number"){
				var time = options.expires;
				var expires = options.expires = new Date();
				expires.setTime(expires.getTime() + time * 1000);
			}
			document.cookie = encodeURIComponent(String(history)) + "=" + encodeURIComponent(String(value)) + (options.expires ? "; expires=" + options.expires.toUTCString() : "") + (options.path ? "; path=" + options.path : "") + (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : "");
		}
	}
	
	// historyIds = JSON.stringify(historyIds);
	
	function get_history(){
		$.ajax({
			url: "goods.php",
			type: "POST",
			data: {act:"history", ids:historyIds},
			dataType: "json",
			cache: false,
			success: function(data) {
				var goods = data;
				var html = '';
				html += '<ul id="top10">';
				for(var i=0,len=goods.length; i<len; i++){
					html += '<li><div class="first"><div class="fl">';
					html += '<font style="color:#424242; font-weight:bold">' + (i*1 + 1*1) + '.' + '</font> <a href="goods.php?goods_id=' + goods[i]['goods_id'] + '" title="" style="color:#424242">' + goods[i]['goods_name'] + '</a>';
					html += '</div></div></li>';
				}
				html += '</ul>';
				console.log(html);
				$("#history").html(html);
			}
		});
	}
	
	get_history();
	
	/*function removeCookie2() {
		alert('fafa');
	}*/
	
	var $clearHistory = $("#clearHistory");
	// 清空浏览记录
	$clearHistory.click(function() {
		$("#history").empty();
		removeCookie("history", "");
	});
	
	//移除Cookie
	function removeCookie(name, options) {
		addCookie(name, null, options);
	}
	

	
	//移除Cookie
	/*function removeCookie(history, options) {
		alert('fafa');
		addCookie(history, null, options);
		$("#history").html();
		return;
	}*/
	
	
	// 清空浏览记录
	/*$clearHistoryProduct.click(function() {
		$historyProduct.empty();
		$(this).text("${message("shop.product.noHistoryProduct")}");
		removeCookie("history", {path: "${base}/"});
	});
	
	*/
	
}



